<template>
    <el-row class="user-info-form" type="flex" justify="center">
        <el-col>
            <el-form label-position="top" :model="editForm" :rules="editFormRules" ref="editForm" @submit.native.prevent>
                <input type="hidden"  v-model="editForm.id">
                <el-form-item label="巡检路线名称：" prop="name">
                    <el-input v-model="editForm.name" auto-complete="off" placeholder="请输入路线名称" clearable></el-input>
                </el-form-item>
                <el-form-item label="类型：" prop="routeTypeId">
                  <el-select filterable v-model="editForm.routeTypeId">
                    <el-option
                      v-for="item in typetOptions"
                      :key="item.dictKey"
                      :label="item.dictValue"
                      :value="item.dictKey">
                    </el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="责任人：">
                  <el-select filterable v-model="checkedUsers" multiple>
                    <el-option
                      v-for="item in accountOptions"
                      :key="item.accountName"
                      :label="item.name"
                      :value="item.accountName">
                    </el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="巡检点位：">
                  <el-select filterable v-model="checkedPointIds" multiple>
                    <el-option
                      v-for="item in pointOptions"
                      :key="item.id.toString()"
                      :label="item.name"
                      :value="item.id.toString()">
                    </el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="备注：">
                    <el-input v-model="editForm.remark" auto-complete="off" type="textarea"></el-input>
                </el-form-item>
                <el-form-item label-width="0" class="verifyidentity-button">
                    <el-button round @click="closeDialog()">取消</el-button>
                    <el-button type="primary" round @click="savePointType('editForm')">确定</el-button>
                </el-form-item>
            </el-form>
        </el-col>
    </el-row>
</template>

<script>
import util from '@/utils/util'
import { getUnitUserList } from '@/api/user'
import { getDictByCode} from '@/api/dic'
import { getFbsTree} from '@/api/fbs'
import * as Dao from '@/api/inspect'
import TreeSelect from '@/libs/TreeSelect/Main'

export default {
  name:'InspectAreaForm',
  components: { 
    TreeSelect
  },
  props: {
    editInfo: {
        type: Object,
        required: false
    },
    organizationId: {},
    closeDialog: {
        type: Function,
        required: true
    },
  },
  data() {
    return {
      buildList:[],
        util:util,
        typetOptions:[],
        pointOptions:[],
        accountOptions:[],
        checkedPointIds:[],
        checkedUsers:[],
        editForm: {
            id: '',
            orgId: this.organizationId || this.$store.state.userinfo.org.id,
            pointIds:'',
            name: '',
            routeTypeId:'',
            dutyUserAccounts:'',
            remark:''
        },
        editFormRules: {
            routeTypeId: [
                { required: true, message: '请选择路线类型', trigger: 'blur' }
            ],
            name: [
                { required: true, message: '请输入路线名称', trigger: 'blur' },
            ],
        },
    }
  },
  created() {
      let infoObj = this.editInfo
      if(infoObj.id) {//编辑
          this.editForm.id = infoObj.id
          this.editForm.orgId = infoObj.orgId
          this.editForm.pointIds = infoObj.pointIds
          this.editForm.name = infoObj.routeName
          this.editForm.routeTypeId = infoObj.routeTypeId.toString()
          this.checkedUsers = infoObj.dutyUserAccounts.toString() != '' ? infoObj.dutyUserAccounts.toString().split(',') : []
          this.checkedPointIds = infoObj.pointIds.toString() != '' ? infoObj.pointIds.toString().split(',') : []
          this.editForm.dutyUserAccounts = infoObj.dutyUserAccounts
          this.editForm.remark = infoObj.remark
      }
      this.getData()
  },
  methods: {
    getData() {
      let that = this
      Dao.getInspectPointNewList({
        queryType : 0,
        orgIds: that.editForm.orgId
      }).then((data)=>{
        that.pointOptions.push.apply(this.pointOptions,data.data);
      })
      //绑定用户下拉框
      getUnitUserList({orgId:that.editForm.orgId}).then((data)=>{
        this.accountOptions.push.apply(this.accountOptions,data.data);
      })
      getDictByCode({code: 92}).then((data)=>{
        this.typetOptions.push.apply(this.typetOptions,data.data.dictValueObj);
      })
    },
    savePointType(formName) {
        // 阻止事件冒泡
        event.stopPropagation()
        let that = this
        that.$refs[formName].validate((valid) => {
            if (valid) {
                let parames = that.editForm
                parames.dutyUserAccounts = that.checkedUsers.join(",")
                parames.pointIds = that.checkedPointIds.join(",")
                Dao.addInspectRoute(parames).then(res => {
                  if( res.status === 200) {
                    that.$message({
                        message: '保存成功！',
                        type: 'success'
                    })
                  }else{
                    this.$message.error(res.info);
                  }
                  that.closeDialog(true)
                });
            } else {
                return false
            }
        });
    },
  }
}
</script>